package com.bjpowernode.gateway.web.controller;

import com.bjpowernode.gateway.web.model.dto.KuaiQianResultDto;
import com.bjpowernode.gateway.web.model.query.KqNoticeQeury;
import com.bjpowernode.gateway.web.model.query.NoticeBaseQeury;
import com.bjpowernode.gateway.web.service.PaymentService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 接收 异步通知 控制接口
 */
@RestController
@Slf4j
public class ReceiveNotifyController {

    @Resource(name = "KUAIQIAN")
    private PaymentService paymentService;

    @GetMapping("/receive/notify")
    public String receiverKqNotify(KqNoticeQeury receiveQuery) {
        log.info("接收第三方支付平台 异步通知传过来的数据 : {}" , receiveQuery);

        KuaiQianResultDto resultDto = new KuaiQianResultDto();
        try {
            // 验证签名
            boolean flag = receiveQuery.checkSignMsg();
            flag = true;
            if (!flag) {
                throw new RuntimeException("签名异常");
            }
            // 签名没有问题 , 验证通知参数 ---> 跟新支付结果
            resultDto = paymentService.checkHandleNotify(receiveQuery);
        } catch (RuntimeException e) {
            resultDto = new KuaiQianResultDto("ORDER_EXCUPTION", "http://localhost:8001/gateway/show.html");
            e.printStackTrace();
        }

        // 根据验证通知 , 确定是否验证成功 , 成功后 , 给客户发送异步通知 , 跳转到指定客户的页面上
        if (resultDto.equals("ORDER_EXCUPTION") || resultDto.equals("ORDER_NONE")) {
            //  重定向 给 redirecturl是 商家页面地址
            return "<result>0</result><redirecturl>"+resultDto.getPageUrl()+"</redirecturl>";
        } else {
            return "<result>1</result><redirecturl>"+resultDto.getPageUrl()+"</redirecturl>";
        }
    }
}
